package online.foxnull2.foxmall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.foxnull2.foxmall.member.entity.MemberEntity;
import online.foxnull2.foxmall.member.entity.MemberLevelEntity;
import online.foxnull2.foxmall.member.exception.EmailExistException;
import online.foxnull2.foxmall.member.exception.UsernameExistException;
import online.foxnull2.foxmall.member.mapper.MemberMapper;
import online.foxnull2.foxmall.member.service.MemberLevelService;
import online.foxnull2.foxmall.member.service.MemberService;
import online.foxnull2.foxmall.member.utils.PageUtils;
import online.foxnull2.foxmall.member.utils.Query;
import online.foxnull2.foxmall.member.vo.MemberLoginVo;
import online.foxnull2.foxmall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements MemberService {

    private MemberLevelService memberLevelService;

    @Autowired
    public void setMemberLevelService(MemberLevelService memberLevelService) {
        this.memberLevelService = memberLevelService;
    }

    private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>().select("id", "level_id", "username", "nickname", "mobile",
                        "email", "header", "gender", "birth", "city", "job", "sign", "source_type", "integration",
                        "growth", "status", "create_time")
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberRegisterVo memberRegisterVo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity memberLevelEntity = memberLevelService.getOne(new QueryWrapper<MemberLevelEntity>().eq("default_status", 1));
        if (!ObjectUtils.isEmpty(memberLevelEntity)) {
            memberEntity.setLevelId(memberLevelEntity.getId());
        }
        checkEmailUnique(memberRegisterVo.getEmail());
        checkUsernameUnique(memberRegisterVo.getUsername());

        memberEntity.setUsername(memberRegisterVo.getUsername());
        memberEntity.setPassword(passwordEncoder.encode(memberRegisterVo.getPassword()));
        memberEntity.setEmail(memberRegisterVo.getEmail());
        baseMapper.insert(memberEntity);
    }

    @Override
    public MemberEntity login(MemberLoginVo memberLoginVo) {
        String username = memberLoginVo.getUsername();
        String password = memberLoginVo.getPassword();
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("email", username).or().eq("username", username));
        if (!ObjectUtils.isEmpty(memberEntity) &&
                passwordEncoder.matches(password, memberEntity.getPassword())) {
            return memberEntity;
        }
        return null;
    }

    private void checkEmailUnique(String email) {
        Long res = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("email", email));
        if (res > 0) {
            throw new EmailExistException();
        }
    }

    private void checkUsernameUnique(String username) {
        Long res = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if (res > 0) {
            throw new UsernameExistException();
        }
    }
}